/**
 *ListPane.java
 *@anthor Daniel Liu
 *2006-5-9 ����01:59:24
 **/
package org.lex.swing.fontchooser;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.beans.PropertyChangeListener;
import java.util.Arrays;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListPane extends JPanel {
	private JTextField textField = null;
	private JScrollPane scrollPane = null;
	private JList list = null;
	private JPanel topPane = null;
	private JLabel titleLabel = null;
	private String propertyName = null;
	private Object oldValue = null;

	public ListPane() {
		super();
		initialize();
	}

	public String getPropertyName() {
		return this.propertyName;
	}

	public void setPropertyName(String name) {
		this.propertyName = name;
	}

	public void setTitle(String title) {
		this.titleLabel.setText(title);
	}

	public String getText() {
		return this.getTextField().getText();
	}

	public void setListData(Comparable... data) {
		if (null == data)
			return;
		Arrays.sort(data);
		this.getList().setListData(data);
	}

	public void setSelectedValue(Object item) {
		this.getList().setSelectedValue(item, true);
	}

	public void addPropertyChangeListener2(PropertyChangeListener listener) {
		this.addPropertyChangeListener(this.propertyName, listener);
	}

	public JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
		}
		return textField;
	}

	public JList getList() {
		if (list == null) {
			list = new JList();
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			list.addListSelectionListener(new ListSelectionHandler());
		}
		return list;
	}

	private void initialize() {
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setHgap(0);
		borderLayout.setVgap(0);
		this.setLayout(borderLayout);
		this.setSize(180, 200);
		this.add(getTopPane(), java.awt.BorderLayout.NORTH);
		this.add(getScrollPane(), java.awt.BorderLayout.CENTER);
	}

	private JPanel getTopPane() {
		if (topPane == null) {
			titleLabel = new JLabel();
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(1);
			topPane = new JPanel();
			topPane.setLayout(gridLayout);
			topPane.add(titleLabel, null);
			topPane.add(getTextField(), null);
		}
		return topPane;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getList());
		}
		return scrollPane;
	}

	class ListSelectionHandler implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (true == e.getValueIsAdjusting())
				return;

			Object newValue = list.getSelectedValue();
			if (null == newValue)
				return;

			textField.setText(String.valueOf(newValue));
			firePropertyChange(propertyName, oldValue, newValue);
			oldValue = newValue;
		}
	}
}
